package com.example.myspringbootpj.controller;

import com.example.myspringbootpj.entity.Manage;
import com.example.myspringbootpj.entity.User;
import com.example.myspringbootpj.mapper.AdminMapper;
import com.example.myspringbootpj.mapper.UserMapper;
import com.example.myspringbootpj.mapper.WordMapper;
import com.example.myspringbootpj.service.SelectService;
import com.example.myspringbootpj.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class adminController {


    private final WordMapper wordMapper;
    private final UserService userService;
    private final UserMapper userMapper;
    private SelectService selectService;
    private final AdminMapper adminMapper;
    @Autowired
    public adminController(UserService userService, AdminMapper adminMapper, UserMapper userMapper, WordMapper wordMapper) {
        this.userService = userService;
        this.adminMapper = adminMapper;
        this.selectService = new SelectService(adminMapper);
        this.userMapper = userMapper;
        this.wordMapper = wordMapper;
    }
    //管理员登录
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestParam("id") String Mid, @RequestParam("password") String password, HttpSession session) {
        Manage findM = adminMapper.selectManage(Mid, password);

        if (findM == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("管理员用户名或者密码不行");
        }

        session.setAttribute("adminId", findM.getManage_id());

        return ResponseEntity.ok("管理去吧！");
    }

    //管理员封号
    @PostMapping("/ban")
    public ResponseEntity<String> banUser(@RequestParam String user_id, @RequestParam String user_not_use , HttpSession session) {
        String Id = (String) session.getAttribute("adminId").toString();
        if (Id==null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("管理员未登录");
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime banDate = LocalDate.parse(user_not_use, formatter).atStartOfDay();
//        LocalDateTime BanDate = LocalDateTime.parse(user_not_use);
        userService.BanUser(user_id, banDate);
        return ResponseEntity.ok("封号成功╰(*°▽°*)╯" + user_not_use);
    }


    //管理员模糊查找用户名，返回所以
    @PostMapping("/FindAndGetUser")
    public ResponseEntity<String> FindUser(@RequestParam String user_id,HttpSession session) {

        String Id = (String) session.getAttribute("adminId").toString();
        if (Id==null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("管理员未登录");
        }

        List<User> users = userService.findAll(user_id);
        return ResponseEntity.ok(users.toString());
    }


    //管理员查看被ban用户
    @PostMapping("/selectBan")
    public ResponseEntity<?> selectBan(@RequestParam String selectId, HttpSession session) {

        String Id = (String) session.getAttribute("adminId").toString();
        if (Id==null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("管理员未登录");
        }

        List<User> users = selectService.SelectUUU(selectId);
        if (users == null || users.isEmpty()) {
            return ResponseEntity.ok("没有符合条件的用户");
        }
        return ResponseEntity.ok("查看成功"+users); // 将列表转换为 JSON
    }

    //封禁用户数量
    @GetMapping("/getAllBan")
    public int getBan(HttpSession session) {

        String Id = (String) session.getAttribute("adminId").toString();
        if (Id==null) {
            return -1;
        }

        LocalDate now = LocalDate.from(LocalDateTime.now());
        int allUser = userMapper.selectAllBan(now);
        return allUser;
    }

    //用户总数
    @GetMapping("/getAllUsers")
    public int getAllUsers(HttpSession session){
        String Id = (String) session.getAttribute("adminId").toString();
        if (Id==null) {
            return -1;
        }

        int allUsers = userMapper.selectAllUserCount();
        return allUsers;
    }

    //单词总数
    @GetMapping("/getAllWordCount")
    public int getAllWordCount(HttpSession session){

        String Id = (String) session.getAttribute("adminId").toString();
        if (Id==null) {
            return -1;
        }
        int AllWords = wordMapper.getWordCount();
        return AllWords;
    }

    //删除用户
    @PostMapping("/deleteUser")
    public String deleteUser(@RequestParam String user_id,HttpSession session) {

        String Id = (String) session.getAttribute("adminId").toString();
        if (Id==null) {
            return "管理员未登录";
        }
        String userid = String.valueOf(adminMapper.deleteUser(user_id));
        return userid;
    }

}
//检查是否封禁

//    public ResponseEntity<String> userInfo(@RequestParam String user_id, @RequestParam String user_password){
//        if(userService.IsBan(user_id)){
//            return ResponseEntity.status(HttpStatus.FORBIDDEN).body("您已被封号，请联系管理员查看原因喵");
//        }
//
//
//    }

